import { Injectable } from '@nestjs/common';
import { CreateAcrossDto } from './dto/create-across.dto';
import { UpdateAcrossDto } from './dto/update-across.dto';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class AcrossService {
  constructor(private prisma: PrismaService) {}

  async createMany(createAcrossDtoArr: CreateAcrossDto[]) {
    if (!createAcrossDtoArr.length) return;

    const { zoneId } = createAcrossDtoArr[0];
    const bets = await this.prisma.bet.findMany({ where: { zoneId } });

    const newArr = createAcrossDtoArr.map((across) => {
      const betId = bets.find((b) => b.btName === across.btName).id;
      return { ...across, betId };
    });

    return this.prisma.across.createMany({ data: newArr });
  }

  async removeMany(id: number) {
    return await this.prisma.across.deleteMany({ where: { id } });
  }

  async findAll(args: Record<string, any>) {
    const { zoneId } = args;
    return await this.prisma.across.findMany({
      where: { zoneId: +zoneId },
      include: { PointAcross: true },
    });
  }

  findOne(id: number) {
    return `This action returns a #${id} across`;
  }

  update(id: number, updateAcrossDto: UpdateAcrossDto) {
    return `This action updates a #${id} across`;
  }

  remove(id: number) {
    return `This action removes a #${id} across`;
  }
}
